import 'package:im_app/im/pojo/ImPojo.dart';
import 'package:sqflite/sqflite.dart';

import '../pojo/UiPojo.dart';
import '../util/ImSingleton.dart';
import '../util/MapUtil.dart';
import 'BaseDao.dart';
import 'ImGroupDao.dart';

final class ImContentSearchDao extends BaseDao {

  Future<List<SearchResult>> searchUsers(String searchContent) async {
    Database? database = getDatabase(); //ImSingleton.getInstance()?.dataBase;
    String sql = 'select u.userId, u.userName, u.headImg from im_user u where status = ? and (u.userName like ? or u.userId '
        'like ?)';
    String param = '%$searchContent%';
    List<Map<String, Object?>>? list = await database?.rawQuery(sql, [ImUserStatus.enable.value, param, param]);
    List<SearchResult> results = list != null ? list.map((map){return _toSearchResult(map);}).toList() : [];
    for (var result in results) {
      var user = ImUser(result.userId!);
      user.userName = result.userName!;
      user.headImg = result.headImg;
      result.user = user;
    }
    return results;
  }

  Future<List<SearchResult>> searchGroups(String userId, String searchContent) async {
    Database? database = getDatabase(); //ImSingleton.getInstance()?.dataBase;
    String sql = '''
     select code groupCode, name groupName, null userId, null userName from im_group where currentUserId = ? and name like ?
     union
     select g.code groupCode, g.name groupName, u.userId userId, u.userName userName from im_group g, im_group_user gu, im_user u where g.id = gu.groupId
        and gu.userId = u.userId and g.currentUserId = ? and u.userName like ?
     ''';
    List<Map<String, Object?>>? list = await database?.rawQuery(sql, [userId, '%$searchContent%', userId, '%$searchContent%']);
    List<SearchResult> result = list != null ? list.map((map){return _toSearchResult(map);}).toList() : [];
    return result;
  }

  Future<List<SearchResult>> searchMessages(String userId, String searchContent) async {
    Database? database = getDatabase(); //ImSingleton.getInstance()?.dataBase;
    String sql = 'select m.sessionId sessionId, count(*) messageTotal from im_message m where m.currentUserId = ? and m.content like ? group by m.sessionId';
    List<Map<String, Object?>>? list = await database?.rawQuery(sql, [userId, '%$searchContent%']);
    List<SearchResult> result = list != null ? list.map((map){return this._toSearchResult(map);}).toList() : [];
    return result;
  }

  SearchResult _toSearchResult(Map<String, Object?> data) {
    var searchResult = SearchResult();
    searchResult.userId = MapUtil.getStringOrNull(data, 'userId');
    searchResult.userName = MapUtil.getStringOrNull(data, 'userName');
    searchResult.groupCode = MapUtil.getStringOrNull(data, 'groupCode');
    searchResult.groupName = MapUtil.getStringOrNull(data, 'groupName');
    searchResult.sessionId = MapUtil.getIntOrNull(data, 'sessionId');
    searchResult.messageTotal = MapUtil.getIntOrNull(data, 'messageTotal');
    return searchResult;
  }

  Future<void> loadGroupInfo(String userId, SearchResult searchResult) async {
    var groupDao = ImGroupDao();
    var group = await groupDao.getByCodeAndUserId(searchResult.groupCode!, userId);
    groupDao.loadGroupUsers(group!);
    searchResult.group = group;
  }
}